import { NoAuth } from '@src/components/Auth';
import { IUser } from '@src/types/account';
import { notification } from 'antd';

// 检测是否有权限
export const checkAuth =
  (fn: (...args: any[]) => any, user: IUser, preventDefault = true) =>
  (...args: any) => {
    const openNotification = () => {
      notification.open({
        message: '站内提示',
        description: <NoAuth />,
        placement: 'top',
        duration: 4,
        style: {
          minWidth: 600,
        },
      });
    };

    // 没权限就返回没权限
    if (!user.uid) {
      openNotification();
      // 是否阻止默认行为，默认都阻止默认行为
      if (preventDefault) {
        const event = args[0] || {};
        event.cancelBubble = true; //ie阻止冒泡行为
        event?.stopPropagation?.(); //Firefox阻止冒泡行为
        event?.preventDefault(); //取消事件的默认动作*换行
      }
      return;
    }

    fn?.(...args);
  };
